﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace Core.M3U8MediaInfo
{
    public static class M3U8MediaInfo
    {
        public const string ExtM3UHeader = "#EXTM3U";
        public const string ExtM3UInf = "#EXTINF:";
        public const string ExtM3UInfDelim = ",";
        public const string FileExtension = "#EXT-X-STREAM-INF";
        public const string M3UCommentDelim = "#";

        public static List<string> MediaParseWithKey(List<string> lstStr)
        {
            List<string> ls = new List<string>();
            decimal Duration = 0;
            try
            {
                for (int i = 0; i < lstStr.Count; i++)
                {
                    if (lstStr[i] == "#EXT-X-ENDLIST") break;
                    if (lstStr[i].IndexOf("#EXT-X-KEY:") >= 0)
                        ls.Add(lstStr[i].Replace("#EXT-X-KEY:", ""));
                    if (lstStr[i].IndexOf("EXTINF") >= 0)
                    {
                        Duration += Convert.ToDecimal(lstStr[i].Replace("#EXTINF:", "").Replace(",", ""));
                        ls.Add(lstStr[i + 1]);
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
            return ls;
        }
        public static List<string> MediaParse(string str)
        {
            List<string> ls = new List<string>();

            try
            {
                string[] tempArr = Regex.Split(str, "\n");
                for (int i = 0; i < tempArr.Length; i++)
                {
                    if (tempArr[i] == "#EXT-X-ENDLIST") break;
                    if (tempArr[i].IndexOf("EXTINF") >= 0)
                    {
                        ls.Add(tempArr[i + 1]);
                    }
                }
            }
            catch (Exception)
            {
                return null;
            }
            return ls;

        }
        public static List<M3UFileInfo> FileParse(string str)
        {
            List<M3UFileInfo> lm3 = new List<M3UFileInfo>();
            string[] tempArr = Regex.Split(str.Replace("\"", ""), "\n");
            for (int i = 0; i < tempArr.Length; i++)
            {
                M3UFileInfo m3f = new M3UFileInfo();
                if (tempArr[i].IndexOf(FileExtension) >= 0)
                {
                    string temp = tempArr[i].Replace(FileExtension + ":", "");
                    m3f.ADDRESS = tempArr[i + 1];
                    foreach (string item in Regex.Split(temp, ExtM3UInfDelim))
                    {
                        if (item.IndexOf("PROGRAM-ID") >= 0)
                        {
                            m3f.PROGRAM_ID = item.Replace("PROGRAM-ID=", "");
                        }
                        if (item.IndexOf("BANDWIDTH") >= 0)
                        {
                            m3f.BANDWIDTH = Convert.ToInt32(item.Replace("BANDWIDTH=", ""));
                        }
                        if (item.IndexOf("RESOLUTION") >= 0)
                        {
                            m3f.RESOLUTION = item.Replace("RESOLUTION=", "");
                        }
                        if (item.IndexOf("NAME") >= 0)
                        {
                            m3f.NAME = item.Replace("NAME=", "");
                        }
                    }
                    lm3.Add(m3f);
                }

            }

            return lm3;
        }
    }
    public class M3UFileInfo
    {
        public string FILENAME { get; set; }
        public int BANDWIDTH { get; set; }
        public string RESOLUTION { get; set; }
        public string NAME { get; set; }
        public string PROGRAM_ID { get; set; }
        public string ADDRESS { get; set; }
        public string SUBTITLE { get; set; }
        public List<string> M3U8FILE { get; set; }
    }
}
